package controllers

import (
	"crypto/md5"
	"github.com/gin-gonic/gin"
	"path/filepath"
)

type File struct {
}

func (File) UploadFile(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(400, gin.H{"message": "文件无效"})
		return
	}
	filename := string(md5.New().Sum([]byte(file.Filename))) + filepath.Ext(file.Filename)
	filePath := "uploads/" + filename

	if err := c.SaveUploadedFile(file, filePath); err != nil {
		c.JSON(500, gin.H{"message": "文件保存失败"})
		return
	}

	c.JSON(200, gin.H{"url": "/" + filePath})
}
